package com.vfa.speech;

import com.vfa.vocaburay.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class SpeechText extends Activity implements OnInitListener{
	
	  private static final String TAG = "TextToSpeechDemo";

	    private TextToSpeech mTts;
	    private Button speech;
	    private EditText enterText;
	    
	    private int MY_DATA_CHECK_CODE = 0;

	    
	    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.speechtext);
		speech = (Button)findViewById(R.id.btn_speech);
		enterText = (EditText)findViewById(R.id.editText_enterTextToSpeech);
	
		// Initialize text-to-speech. This is an asynchronous operation.
        // The OnInitListener (second argument) is called after initialization completes.

        // The button is disabled in the layout.
        // It will be enabled upon initialization of the TTS engine.
        
	    // QUEUE_ADD: the new entry is added at the end of the playback queue
	    //QUEUE_FLUSH: all entries in the playback queue (media to be played and text to be synthesized) are dropped and replaced by the new entry
		
        speech.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	 String text = enterText.getText().toString().trim();
            	 
			       if (text!=null && text.length()>0) {
			    	   Toast.makeText(SpeechText.this, "Saying: " + text, Toast.LENGTH_LONG).show();
			    	   
			    	   mTts.speak(text, TextToSpeech.QUEUE_ADD, null);
			       }
            }
        });
        
        Intent checkIntent = new Intent();
	      checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
	      startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

	}
	
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
            	mTts = new TextToSpeech(this, this);
            } 
            else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }

    }

    @Override
    public void onInit(int status) {        
        if (status == TextToSpeech.SUCCESS) {
            System.out.println("Text-To-Speech engine is initialized"); 
        }
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(SpeechText.this, 
                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        }
    }

}
